package com.work.music.dao;

import com.work.music.entity.Admin;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

@Repository
public interface AdminMapper {
    @Delete({
        "delete from admin",
        "where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
        "insert into admin (admin_name, admin_pwd, ",
        "addtime, type)",
        "values (#{adminName,jdbcType=VARCHAR}, #{adminPwd,jdbcType=VARCHAR}, ",
        "#{addtime,jdbcType=TIMESTAMP}, #{type,jdbcType=INTEGER})"
    })
    @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Long.class)
    int insert(Admin record);

    @Select({
        "select",
        "id, admin_name, admin_pwd, addtime, type",
        "from admin",
        "where id = #{id,jdbcType=BIGINT}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
        @Result(column="admin_name", property="adminName", jdbcType=JdbcType.VARCHAR),
        @Result(column="admin_pwd", property="adminPwd", jdbcType=JdbcType.VARCHAR),
        @Result(column="addtime", property="addtime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="type", property="type", jdbcType=JdbcType.INTEGER)
    })
    Admin selectByPrimaryKey(Long id);

    @Select({
        "select",
        "id, admin_name, admin_pwd, addtime, type",
        "from admin"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
        @Result(column="admin_name", property="adminName", jdbcType=JdbcType.VARCHAR),
        @Result(column="admin_pwd", property="adminPwd", jdbcType=JdbcType.VARCHAR),
        @Result(column="addtime", property="addtime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="type", property="type", jdbcType=JdbcType.INTEGER)
    })
    List<Admin> selectAll();

    @Update({
        "update admin",
        "set admin_name = #{adminName,jdbcType=VARCHAR},",
          "admin_pwd = #{adminPwd,jdbcType=VARCHAR},",
          "addtime = #{addtime,jdbcType=TIMESTAMP},",
          "type = #{type,jdbcType=INTEGER}",
        "where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(Admin record);

    @Select({
            "select",
            "id, admin_name, admin_pwd, addtime, type",
            "from admin",
            "where admin_name = #{username,jdbcType=VARCHAR}"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
            @Result(column="admin_name", property="adminName", jdbcType=JdbcType.VARCHAR),
            @Result(column="admin_pwd", property="adminPwd", jdbcType=JdbcType.VARCHAR),
            @Result(column="addtime", property="addtime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="type", property="type", jdbcType=JdbcType.INTEGER)
    })
    Admin selectByName(String username);

    /**
     * 获取总条数
     * @param msg
     * @return
     */
    @Select("select ifnull(count(*), 0) from admin ${msg}")
    int selectCount(@Param("msg") String msg);

    /**
     * 获取列表
     * @param msg
     * @param limit
     * @param offset
     * @return
     */
    @Select("select id,admin_name adminName,addtime,type from admin ${msg} " +
            "order by addtime desc limit #{limit},#{offset}")
    List<Map<String, Object>> selectList(@Param("msg") String msg, @Param("limit") int limit, @Param("offset") int offset);
}